﻿namespace RulesEngine
{
    public class RuleGroup<TCandidate>
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public RuleSet<TCandidate> RuleSet { get; set; }
        public List<IRule<TCandidate>> Rules { get; set; }
        public IGroupResultProcessor<TCandidate> ResultProcessor { get; set; }

        public RuleGroup()
        {
            Rules = new List<IRule<TCandidate>>();
        }

        public bool Execute(TCandidate candidate)
        {
            IRule<TCandidate> rule = GetCompositeRule();
            if (rule == null)
            {
                return false;
            }

            bool result = rule.Execute(candidate);
            return result;
        }

        public IRule<TCandidate> GetCompositeRule()
        {
            IRule<TCandidate> compositeRule = null;

            foreach (IRule<TCandidate> rule in Rules)
            {
                if (compositeRule == null)
                {
                    compositeRule = rule;
                }
                else
                {
                    compositeRule = compositeRule.And(rule);
                }
            }

            return compositeRule;
        }
    }

    public interface IGroupResultProcessor<TCandidate>
    {
        object GetResult(TCandidate candidate);
    }
}